﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class MasterAR : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Persons currentPerson = new Persons();

        if (Session["CurrentPerson"] != null)
            currentPerson = GlobalVars.getCurrentPersonAR();
        if (!Page.IsPostBack)
        {

           

            if (currentPerson.GenID != -1)
            {
                lblUserName.Text = currentPerson.Name_AR + " " + currentPerson.FamilyName_AR;

                //if (currentPerson.ID == "")
                //{
                //    lblUserName.Text = lblUserName.Text + " " + currentPerson.objOrgnizationChart.OrgCode;  
                //}

            }
            else
            {
                lblUserName.Visible = false;
                lblWelcome.Visible = false;
                lnkLogout.Visible = false;
            }
            checkSecurityAndCustomizePage(currentPerson);



            setLanguageLink();

            if (Application["CurrentCount"] == null)
                         
            {
                try
                {


                    Application.Lock();

                    Application["CurrentCount"] = 0;

                    Application.UnLock();
                }
                catch
                {
                }
            }
            
            
            lblNowValue.Text = Application["CurrentCount"].ToString();
            lblHistoryValue.Text = DatabaseComponent.HistoryVisits.MyCount.ToString();

            lblMax.Text = MaxVisitsDate.objMax.MaxConcurrenceNumber.ToString();
            lblMaxDate.Text = MaxVisitsDate.objMax.CurrentDate.ToString("dd/MM/yyyy HH:mm");

            aHrefMailLink.HRef = "MailTo:" + GlobalVars.getSenderMail();
        }




            
    }


    /// <summary>
    /// this function will set the langauge link to the other language page
    /// </summary>
    private void setLanguageLink()
    {
        try
        {

            string url = Page.Request.Url.AbsoluteUri;
            string urlOnly1 = "";
            string urlParams = "";

            try
            {
                urlOnly1 = url.Substring(0, url.ToLower().LastIndexOf("ar.aspx"));

            }
            catch
            {


                throw new Exception();
            }

            try
            {
                urlParams = url.Substring(urlOnly1.Length + 7);
            }
            catch
            {
            }

            url = "./" + urlOnly1.Substring(urlOnly1.LastIndexOf("/") + 1) + "EN.aspx" + urlParams;

            aEnglish.HRef = url;

        }
        catch
        {
        }
    }

    /// <summary>
    /// it will check the security and customize the page
    /// </summary>
    /// <param name="currentPerson">the person to set security to his previleges</param>
    protected void checkSecurityAndCustomizePage(Persons currentPerson)
    {
        //Security Check

        //if (currentPerson.objUserType == null || currentPerson.objUserType.TypeNameEN == "Employee")
        //{
            
        //    dvAccounts.Visible = false;
        //    dvReports.Visible = false;
        //    dvSupportControl.Visible = false;
        //    dvCatalogManager.Visible = false;

        //}
        //else if (currentPerson.objUserType.TypeNameEN == "Operator")
        //{

        //    dvReports.Visible = false;



        //}
        //else
        //{
        //}


        //if (currentPerson.objPersonType == null)
        //{
        //    dvManager.Visible = false;
        //}


        //else if (currentPerson.objPersonType.EmployeeTypeNameEN == "Manager")
        //{
        //    dvManager.Visible = true;
        //    dvProfile.Visible = false;
        //}
        //else if (currentPerson.objPersonType.EmployeeTypeNameEN == "Employee")
        //{
        //    dvManager.Visible = false;
        //    dvProfile.Visible = false;
        //}
        //else if (currentPerson.objPersonType.EmployeeTypeNameEN == "Family")
        //{
        //    dvManager.Visible = false;
            
        //}


        //if (currentPerson.lstAllRulesIDs.Count == 0)
        //    Response.Redirect("LoginAR.aspx");


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Home_page_view))
            dvHome.Visible = false;
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Catalog_Page))
            dvCatalog.Visible = false;
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Registered_courses_page))
            dvRegistered.Visible = false;
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Profile_page))
            dvProfile.Visible = false;
        if ((!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Manager_page)) && currentPerson.lstAddEmployees.Count == 0)
            dvManager.Visible = false;
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Admin_page))
        {
            dvAccounts.Visible = false;
            dvNumvers.Visible = false;
        }
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Reports_page))
            dvReports.Visible = false;
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Ticket_add_page))
            dvSupport.Visible = false;
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Support_control))
            dvSupportControl.Visible = false;
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Questions_of_courses_page))
            dvCatalogManager.Visible = false;


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Acounts_page))
            aAccountsViewEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Oranization_chart_page))
            aOrganizationEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Courses_page))
            aCoursesViewEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Folders_page))
            aFoldersEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Ticket_types_page))
            aTicketTypeViewEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Edit_Support_videos))
            aSupportVideoEditEN.Visible = false;


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Edit_Support_Documents))
            aSupportDocumentsEditEN.Visible = false;

        if (currentPerson.UserTypeID != 1)
            aChangeMainMailEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Editing_tichet_types_page))
            aFaqEditEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Hints_page))
            aHintsViewEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Contact_us_edit_page))
            aContactUSEditEN.Visible = false;


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.About_us_page))
            aAboutusEditEN.Visible = false;


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Groups_page))
            aGroupsEditEN.Visible = false;

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Edit_certificate_details))
            aCertificateDataEN.Visible = false;

        if (currentPerson.UserTypeID != 1)
            aPrintCertByAdminEN.Visible = false;


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Change_User_Status_Page))
            aUpdateEmployeeStatusEN.Visible = false;


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Page_of_editing_HelpDescContacts))
            aHelpDescContacts.Visible = false;


        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Home_page_view))
            dvChangePassword.Visible = false;



        if (currentPerson.GenID < 0 || currentPerson.PersonTypeID == 3)
        {
            dvMyFamily.Visible = false;
        }


        if (currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Admin_page))
        {
            dvProfile.Visible = false;
        }

    }


    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        if (Response.Cookies["CurrentPerson"] != null)
        {
            Response.Cookies["CurrentPerson"].Expires = DateTime.Now.AddDays(-1);

        }
        Session["logout"] = true;
        Response.Redirect("LoginAR.aspx");
        

    }
}
